package com.mall.czlr.base.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Slf4j
public class CommonUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static Long time() {
        return System.currentTimeMillis() / 1000;
    }

    public static Integer parseInt(Object obj, Integer optional) {
        if (obj instanceof Long) {
            return ((Long) obj).intValue();
        } else if (obj instanceof Integer) {
            return (Integer) obj;
        } else {
            try {
                return (int) Double.parseDouble(obj.toString());
            } catch (Exception var3) {
                log.error("parse Integer error: {}, source: \n{}", var3.getMessage(), obj);
                return optional;
            }
        }
    }

    public static Integer parseInt(Object obj) {
        return parseInt(obj, 0);
    }

    public static Long parseLong(Object obj, Long optional) {
        if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer) obj).longValue();
        } else {
            try {
                return (long) Double.parseDouble(obj.toString());
            } catch (Exception var3) {
                log.error("parse Long error: {}, source: \n{}", var3.getMessage(), obj);
                return optional;
            }
        }
    }

    public static Long parseLong(Object obj) {
        return parseLong(obj, 0L);
    }

    public static Double parseDouble(Object obj, Double optional) {
        if (obj instanceof Double) {
            return (Double) obj;
        } else {
            try {
                return Double.parseDouble(obj.toString());
            } catch (Exception var3) {
                log.error("parse Double error: {}, source: \n{}", var3.getMessage(), obj);
                return optional;
            }
        }
    }

    public static Double parseDouble(Object obj) {
        return parseDouble(obj, 0.0D);
    }

    public static String generateRandomString(int length) {
        char[] chars = "abcdefghijklmnopqrstuvwxyzABSDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
        return generateRandomString(chars, length);
    }

    public static String generateRandomNumberString(int length) {
        char[] chars = "1234567890".toCharArray();
        return generateRandomString(chars, length);
    }

    private static String generateRandomString(char[] chars, int length) {
        Random r = new Random(System.currentTimeMillis());
        char[] id = new char[length];

        for (int i = 0; i < length; ++i) {
            id[i] = chars[r.nextInt(chars.length)];
        }

        return new String(id);
    }

    public static String implode(List list, String glue) {
        if (list == null || list.size() == 0) {
            return null;
        }

        StringBuilder result = new StringBuilder();
        boolean flag = false;
        for (Object o : list) {
            if (flag) {
                result.append(glue);
            } else {
                flag = true;
            }
            result.append(o.toString());
        }
        return result.toString();
    }

    public static String timeFormat() {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time() * 1000L);
        return formatter.format(date);
    }

    public static String timeFormat(long time) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time * 1000L);
        return formatter.format(date);
    }

    public static String timeFormat(long time, String format) {
        DateFormat formatter = new SimpleDateFormat(format);
        Date date = new Date(time * 1000L);
        return formatter.format(date);
    }

    public static String jsonEncode(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException var2) {
            log.error("json encode error: {}, source: \n{}", var2.getMessage(), object);
            return "";
        }
    }

    private static <T> T getT(String jsonString, Object t, Class<T> type) {
        if (jsonString == null) {
            return null;
        } else {
            return (T) t;
        }
    }

    @Nullable
    public static <T> T jsonDecode(String jsonString, Class<T> type) {
        T object = null;
        try {
            object = (T) getT(jsonString, objectMapper.readValue(jsonString, type), Class.class);
        } catch (Exception var4) {
            log.error("json decode error: {}, source: \n{}", var4.getMessage(), jsonString);
        }
        return object;
    }

    @Nullable
    public static <T> T jsonDecode(String jsonString, TypeReference<T> typeReference) {
        T object = null;
        try {
            object = (T) getT(jsonString, objectMapper.readValue(jsonString, typeReference), TypeReference.class);
        } catch (Exception var4) {
            log.error("json decode error: {}, source: \n{}", var4.getMessage(), jsonString);
        }
        return object;
    }

    @Nullable
    public static <T> T jsonDecode(String jsonString, JavaType javaType) {
        T object = null;
        try {
            object = (T) getT(jsonString, objectMapper.readValue(jsonString, javaType), JavaType.class);
        } catch (Exception var4) {
            log.error("json decode error: {}, source: \n{}", var4.getMessage(), jsonString);
        }
        return object;
    }
}
